package com.logistics.erp.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.logistics.erp.entity.domain.*;
import com.logistics.erp.entity.model.*;
import com.logistics.erp.mapper.ErpOrderMapper;
import com.logistics.utils.constants.Constants;
import com.logistics.utils.exception.MessageException;
import com.logistics.utils.tool.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DS("erp")
@Service
public class ErpOrderService {

    @Autowired
    private ErpOrderMapper erpOrderMapper;

    /**
     * 查找物料信息
     * @param documentNumber
     * @param itemId
     * @param shopOrder
     * @return
     */
    public ErpOrder materialInfo(String documentNumber, String itemId, String shopOrder) throws MessageException {
        Map<String, Object> params = new HashMap<>();
        params.put("documentNumber", StringUtils.isNotNull(documentNumber) ? documentNumber : "");
        params.put("itemId", StringUtils.isNotNull(itemId) ? itemId : "");
        params.put("shopOrder", StringUtils.isNotNull(shopOrder) ? shopOrder : "");
        List<ErpOrder> list = erpOrderMapper.findErpOrderList(params);
        if(list.isEmpty()){
            throw new MessageException("物料信息不存在");
        }
        return list.get(0);
    }

    /**
     * 查找计划数量信息
     * @param billNos
     * @return
     */
    public List<ErpPlanQty> planQty(List<String> billNos){
        return erpOrderMapper.findPlanQty(billNos);
    }

    /**
     * 查找缺料列表
     * @param billNos
     * @return
     */
    public List<ErpLackMaterial> lackMaterialsInfo(List<String> billNos){
        return erpOrderMapper.findLackMaterialInfo(billNos);
    }

    /**
     * 查找拣配数量
     * @param billNos
     * @return
     */
    public List<ErpLackMaterial> pickActualQty(List<String> billNos){
        return erpOrderMapper.findPickActualQty(billNos);
    }

    /**
     * 获取erp拣配实际数量
     * @param billNo
     * @param orderNo
     * @param itemCode
     * @param opNo
     * @return
     */
    public BigDecimal actualQty(String billNo, String orderNo, String itemCode, String opNo, String seqNo){
        Map<String, Object> params = new HashMap<>();
        params.put("billNo", billNo);
        params.put("orderNo", orderNo);
        params.put("itemCode", itemCode);
        params.put("opNo", opNo);
        params.put("seqNo", seqNo);
        return erpOrderMapper.findActualQty(params);
    }

    /**
     * 根据领料单号查找优先级
     * @param code
     * @return
     */
    @DS("erp")
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    public Map<String, Object> priority(String code){
        if(StringUtils.isNull(code)){
            return null;
        }
        return erpOrderMapper.findErpOrderPriority(code);
    }

    /**
     * 根据领料单号查询是否可拣配
     * @param billNo
     * @return
     */
    public Integer isPicking(String billNo){
        return erpOrderMapper.findIsPicking(billNo);
    }

    /**
     * 根据拣配单号 更新状态
     * @param billNo
     */
    public void updatePickQty(BigDecimal pickQty, String billNo, String itemWorkCode,
                                 String itemCode, String seqNo){
        Map<String, Object> params = new HashMap<>();
        params.put("qty", pickQty);
        params.put("billNo", billNo);
        params.put("itemWorkCode", itemWorkCode);
        params.put("itemCode", itemCode);
        params.put("seqNo", seqNo);
        erpOrderMapper.updatePickQty(params);
    }

    /**
     * 根据拣配单号 更新状态
     * @param billNo
     */
    public void updatePickQty(BigDecimal pickQty, String billNo, String itemCode){
        Map<String, Object> params = new HashMap<>();
        params.put("qty", pickQty);
        params.put("billNo", billNo);
        params.put("itemCode", itemCode);
        erpOrderMapper.updatePickQtyPaint(params);
    }

    /**
     * 更改拣配数量
     * @param pickQty
     * @param id
     */
    public void updateOrderDetailPickQty(BigDecimal pickQty, String id){
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("pickQty", pickQty);
        erpOrderMapper.updateOrderDetailPickQty(params);
    }

    /**
     * 更新拣配状态
     * @param ids
     */
    public void updateOrderDetailPickStatus(List<String> ids){
        erpOrderMapper.updateOrderDetailStatus(ids);
    }

    /**
     * 更新配送时间
     * @param id
     */
    public void updateOrderDetailDeliveryTime(String id){
        erpOrderMapper.updateOrderDetailDeliveryTime(id);
    }

    /**
     * 更新状态
     * @param billNos
     */
    public void updatePickStatus(List<String> billNos){
        erpOrderMapper.updatePickStatus(billNos);
    }

    /**
     * 更新状态
     */
    public void updatePickStatus(String billNo, String itemCode){
        erpOrderMapper.updatePickStatusPaint(billNo,itemCode);
    }

    /**
     * 更新配送时间
     * @param billNo
     */
    public void updateDeliveryTime(String billNo, String itemWorkCode,
                                   String itemCode, String seqNo){
        Map<String, Object> params = new HashMap<>();
        params.put("billNo", billNo);
        params.put("itemWorkCode", itemWorkCode);
        params.put("itemCode", itemCode);
        params.put("seqNo", seqNo);
        erpOrderMapper.updateDeliveryTime(params);
    }

    /**
     * 更新配送时间
     * @param billNo
     */
    public void updateDeliveryTime(String billNo, String itemCode){
        Map<String, Object> params = new HashMap<>();
        params.put("billNo", billNo);
        params.put("itemCode", itemCode);
        erpOrderMapper.updateDeliveryPaint(params);
    }

    /**
     * 根据任务号查找流行信息
     * @param taskNo
     * @return
     */
    @DS("erp")
    @Transactional
    public ErpFlow flow(String taskNo, String itemOpNo){
        Map<String, Object> params = new HashMap<>();
        params.put("taskNo", taskNo);
        params.put("itemOpNo", itemOpNo);
        return erpOrderMapper.findErpFlow(params);
    }

    /**
     * 查找任务流行信息
     * @param taskNo 任务编号
     * @return
     */
    public ErpOrder findMaterialInfo(String taskNo){
        return erpOrderMapper.findMaterialInfo(taskNo);
    }

    /**
     * 查找物料流向
     * @param taskNo
     * @return
     */
    public MaterialFlowRow materialFlow(String erpDep, String taskNo){
        Map<String, Object> params = new HashMap<>();
        params.put("erpDep", erpDep);
        params.put("taskNo", taskNo);
        MaterialFlow result = erpOrderMapper.findMaterialFlows(params).stream().findFirst().orElse(null);
        return result == null ? null : MaterialFlowRow.build(result);
    }

    /**
     * 查找暂存数据
     * @param orderNo
     * @param qty
     * @return
     */
    public Integer findNoDemand104T(String orderNo, Integer qty){
        Map<String, Object> params = new HashMap<>();
        params.put("orderNo", orderNo);
        params.put("qty", qty);
        return erpOrderMapper.findNoDemand104T(params);
    }

    /**
     * 更改暂存数据
     * @param erpStashSave
     */
    public void updateNoDemand104T(ErpStashSave erpStashSave){
        erpOrderMapper.updateNoDemand104T(erpStashSave);
    }
    /**
     * 通过任务号进行直投处理
     * @param erpDirectPut
     * @return
     */
    public void directPut(ErpDirectSave erpDirectPut){
        erpOrderMapper.callMaterialDirectPut(erpDirectPut);
    }

    /**
     * 暂存处理
     * @param erpStashSave
     * @return
     */
    public void insertStash(ErpStashSave erpStashSave){
        erpOrderMapper.insertStash(erpStashSave);
    }

    /**
     * 查找ERP用户登录信息
     * @param code
     * @param pwd
     * @return
     */
    public ErpPersonRow userInfoByCodeAndPw(String code, String pwd){
        Map<String, Object> params = new HashMap<>();
        params.put("code", code);
        params.put("pwd", pwd);
        return ErpPersonRow.build(erpOrderMapper.findUserByCodeAndPw(params));
    }

    /**
     * 查找是否是直投部门
     * @param itemCode
     * @return
     */
    public List<ErpDept> depByItemCode(String itemCode){
        return erpOrderMapper.findDeptByItemCode(itemCode);
    }

    /**
     * 根据部门获取需求数量
     * @param deptCodes
     * @return
     */
    public List<MaterialBillRow> repQtyByDept(List<String> deptCodes, String taskNo) {
        return erpOrderMapper.repQtyByDept(deptCodes,taskNo);
    }

    /**
     * 查找来源为工序交接和内部交接
     * @param taskNo 任务号
     * @return
     */
    public List<ErpDeliveryOrderDetail> findSourceFromTakeOver(String taskNo){
        Map<String, Object> params = new HashMap<>();
        params.put("taskNo", taskNo);
        params.put("list", Arrays.asList(Constants.ERP_SOURCE_METHOD_PROCESS_REPORT_TRANSFER, Constants.ERP_SOURCE_METHOD_INSIDE_TRANSFER));
        return erpOrderMapper.findSourceFromTakeOver(params);
    }

    /**
     * 查找是否进行过工序交接
     * @param taskNo 任务号
     * @return
     */
    public Integer countTakeOver(String taskNo){
        return erpOrderMapper.countTakeOver(taskNo);
    }

    /**
     * 查询流向标签（工序交接、内部交接）
     * @param taskNo 任务号
     * @return
     */
    public List<ErpDeliveryOrderDetail> findFlowInfoFromProcessAndInside(String taskNo){
        Map<String, Object> params = new HashMap<>();
        params.put("taskNo", taskNo);
        params.put("list", Arrays.asList(Constants.ERP_SOURCE_METHOD_PROCESS_REPORT_TRANSFER, Constants.ERP_SOURCE_METHOD_INSIDE_TRANSFER));
        return erpOrderMapper.findFlowInfo(params);
    }

    /**
     * 执行工序交接
     * @param taskNo 任务号
     * @return
     */
    public void insertPacRoutingOpProcess(String taskNo){
        erpOrderMapper.insertPacRoutingOpProcess(taskNo);
    }

    /**
     * 执行内部交接
     * @param taskNo 任务号
     * @return
     */
    public void insertPacRoutingOpInside(String taskNo) {
        erpOrderMapper.insertPacRoutingOpInside(taskNo);
    }

    public ErpOrder findMaterialData(String code, String billNo) {
        return erpOrderMapper.findMaterialData(code, billNo);
    }

    /**
     * 根据扫描的任务号，获取任务的物料信息以及提出外协的部门
     * @param taskNo
     * @return
     */
    public PacOptransBillDetail materialByTaskNo(String taskNo) {
        return erpOrderMapper.materialByTaskNo(taskNo);
    }

    /**
     * 外协任务需要质检，判断是否已经质检完成
     * @param taskNo
     * @return
     */
    public QcWxDetailV receptionQuality(String taskNo) {
        return erpOrderMapper.receptionQuality(taskNo);
    }

    /**
     * 全部质检完成,将if_yes 更新为 'Y'
     * @param taskNo
     */
    public void updateIfYes(String taskNo) {
        erpOrderMapper.updateIfYes(taskNo);
    }

    /**
     * 判断任务是否领料齐全
     * @param taskNo
     * @return
     */
    public String pickingComplete(String taskNo) {
        return erpOrderMapper.pickingComplete(taskNo);
    }

    /**
     * 扫描任务号显示的物料信息
     * @param taskNo
     * @return
     */
    public MaterialReception materialReception(String taskNo) {
        return erpOrderMapper.materialReception(taskNo);
    }

    /**
     * 质检完成和领料齐全后，执行以下存储过程
     * @param erpReceptionSave
     */
    public void callMaterialErpReception(ErpReceptionSave erpReceptionSave) {
        erpOrderMapper.callMaterialErpReception(erpReceptionSave);
    }

    /**
     * 验证是否记账成功
     * @param piOrderNoOne
     * @return
     */
    public List<String> statusByOrderNo(String piOrderNoOne) {
        return erpOrderMapper.statusByOrderNo(piOrderNoOne);
    }

    /**
     * erp 获取失败信息
     * @param erpUniqueId
     * @return
     */
    public String findErrMsg(String erpUniqueId) {
        return erpOrderMapper.findErrMsg(erpUniqueId);
    }

    /**
     * 存储过程 inware_tz_autowghb_sp
     * @param piOrderNoOne
     */
    public void callMaterialErpInware(String piOrderNoOne) {
        erpOrderMapper.callMaterialErpInware(piOrderNoOne);
    }

    /**
     * 根据任务号查询 source_method
     * @param piOrderNoOne
     * @return
     */
    public String getsourceMethod(String piOrderNoOne) {
        return erpOrderMapper.getsourceMethod(piOrderNoOne);
    }

    /**
     * 入库打印信息查询
     * @param code
     * @return
     */
    public ErpDeliveryOrderDetail inbound(String code) {
        return erpOrderMapper.inbound(code);
    }
}
